<!doctype html>
<html lang="zh-cmn-Hans" xmlns:th="http://www.thymeleaf.org"
	xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">

<head th:replace="menu/import::semantic">
<title id="title">用户管理-物流系统</title>
</head>
<body>
	<div class="ui main container">
		<div class="ui clearing segment">
			<form th:action="@{/user}" id="filterForm" class="ui form" method="get">
				<div class="fields">
					<div class="field">
						<label>登录名：</label> <input type="text" name="loginName"
							placeholder="" style='width: 150px;' th:value="${user.loginName}" />
					</div>
					<div class="field">
						<label>用户名：</label> <input type="text" name="displayName"
							placeholder="" style='width: 150px;'
							th:value="${user.displayName}" />
					</div>
					<div style="height: 20px; line-height: 80px;">
						<button shiro:hasRole="admin" id="query"
							class="ui red button" type="submit">查询</button>
						<div class="ui buttons">
							<button id="clear" class="ui white button" type="button"
								onclick="$('.ui.form').form('clear')">重置</button>
						</div>
					</div>
				</div>
			</form>
		</div>
		<div class="ui buttons">
			<button shiro:hasRole="admin" class="ui red button"
				type="button" onclick="addUser();">新增</button>
		</div>
		<div class="ui buttons">
			<button shiro:hasRole="admin" class="ui red button"
				type="button" onclick="del();">删除</button>
		</div>
		<div class="ui buttons">
			<button shiro:hasRole="admin" class="ui red button"
				type="button" onclick="isLock('1');">锁定</button>
		</div>
		<div class="ui buttons">
			<button shiro:hasRole="admin" class="ui red button"
				type="button" onclick="isLock('0');">解锁</button>
		</div>
		<div>
			<br>
		</div>
		<div style="overflow: auto;">
			<table class="ui red table" th:fragment="showTable">
				<thead>
					<tr class="single line">
						<th><input id="cbAll" name="cbAll" type="checkbox" /></th>
						<th>登录名</th>
						<th>用户名</th>
						<th>失败次数</th>
						<th>是否锁定</th>
						<th>过期时间</th>
					</tr>
				</thead>
				<tbody th:if="${pageInfo}">
					<tr th:each="user : ${pageInfo.list}">
						<td><input type="checkbox" name="cbOne" th:value="${user.id}" /></td>
						<td shiro:hasRole="admin"><a href="#"
							th:onclick="'noSelectUpdate(\'' + ${user.id} + '\');'"
							th:text="${user.loginName}"></a></td>
						<td shiro:lacksRole="admin" th:text="${user.loginName}"></td>
						<td th:text="${user.displayName}"></td>
						<td th:text="${user.errorCount}"></td>
						<td><span th:if="${user.isLock != null}"
                                    th:switch="${user.isLock.toString()}"> <span
                                        th:case="'0'">否</span> <span
                                        th:case="'1'">是</span> 
                                </span></td>
						<td
							th:text="${#dates.format(user.expireTime,'yyyy-MM-dd HH:mm:ss')}"></td>
					</tr>
				</tbody>
			</table>
		</div>
	</div>

	<script type="text/javascript">
		$(function(){
			$(".ui.dropdown").dropdown();
		});
		
		function isLock(state) {
			var idArray = new Array();
			$('input[name="cbOne"]:checked').each(function(){  
				    idArray.push($(this).val());
			});
			if(idArray.length==0) {
				alert("请至少选择一个复选框！");
				return ;
			}
			//idArray.push(state);
			if(confirm("确定要更改选定用户状态？")) {
				$.ajax({
					type : "GET",
					url : "user/isLock",
					data : {"ids":idArray,"state":state},
					traditional : true, 
					cache : false,
					timeout : 600000,
					success : function(data) {
						alert(data);
						location.href = "";
					},
					error : function(jqXHR, textStatus, errorThrown) {
						alert("error");
						alert(jqXHR.responseText);
					}
				});
			}
		}
		
		function updatePasswordByAdmin() {
			var checkeds = $("input[name='cbOne']:checked");
			var password = $("input[name='password']").val();
		    if(checkeds.length != 1) {
		    	alert("修改密码，必须且只能选择一个复选框！");
		    	return;
		    }
		    if(password == "") {
				alert("密码不能为空！");
				return;
			}
		    var checked = checkeds[0].value;
		    
			$.ajax({
				type : "POST",
				url : contextPath+"/user/updatePasswordByAdmin",
				data : {"userId":checked,"password":password},
				cache : false,
				timeout : 600000,
				success : function(data) {
					alert(data);
					//location.href = "";
				},
				error : function(jqXHR, textStatus, errorThrown) {
					alert("error");
					alert(jqXHR.responseText);
				}
			});
		}
		
		function configRoles() {
			var checkeds = $("input[name='cbOne']:checked");
			
		    if(checkeds.length != 1) {
		    	alert("修改，必须且只能选择一个复选框！");
		    	return;
		    }
		    var checked = checkeds[0].value;
		    
			$.ajax({
				type : "POST",
				url : "user/configRoles",
				data : {"userId":checked},
				cache : false,
				timeout : 600000,
				success : function(data) {
					layer.open({
	            		  type: 1,
	            		  title: '配置用户所属角色页面',
	            		  skin: 'layui-layer-rim', //样式类名
	            		  anim: 2,
	            		  area: ['600px', '300px'], //宽高
	            		  shadeClose: false, //开启遮罩关闭
	            		  content: data,
	            		  btn: '关闭',
	            		  yes: function(){
	            	          layer.closeAll();
	            	        }
	            		});
				},
				error : function(jqXHR, textStatus, errorThrown) {
					alert("error");
					alert(jqXHR.responseText);
				}
			});
		}
		
		function addUser() {
			$.ajax({
				type : "POST",
				url : "user/preAdd",
				cache : false,
				timeout : 600000,
				success : function(data) {
					layer.open({
	            		  type: 1,
	            		  title: '添加用户页面',
	            		  skin: 'layui-layer-rim', //样式类名
	            		  anim: 2,
	            		  area: ['350px', '340px'], //宽高
	            		  shadeClose: false, //开启遮罩关闭
	            		  content: data
	            		});
				},
				error : function(jqXHR, textStatus, errorThrown) {
					alert("error");
					alert(jqXHR.responseText);
				}
			});
		}
		
		function del() {
			var idArray = new Array();
			$('input[name="cbOne"]:checked').each(function(){  
				    idArray.push($(this).val());
			});
			if(idArray.length==0) {
				alert("请至少选择一个复选框！");
				return ;
			}
			if(confirm("确定要删除选中的用户信息？")) {
				$.ajax({
					type : "GET",
					url : "user/delete",
					data : {"ids":idArray},
					traditional : true, 
					cache : false,
					timeout : 600000,
					success : function(data) {
						alert(data);
						location.href = "";
					},
					error : function(jqXHR, textStatus, errorThrown) {
						alert("error");
						alert(jqXHR.responseText);
					}
				});
			}
		}
		
		function updateUser() {
			var checkeds = $("input[name='cbOne']:checked");
		    if(checkeds.length != 1) {
		    	alert("修改，必须且只能选择一个复选框！");
		    	return;
		    }
		    var checked = checkeds[0].value;
		    
			$.ajax({
				type : "POST",
				url : "user/preUpdate",
				data : {"user":checked},
				cache : false,
				timeout : 600000,
				success : function(data) {
					layer.open({
	            		  type: 1,
	            		  title: '修改用户信息页面',
	            		  skin: 'layui-layer-rim', //样式类名
	            		  anim: 2,
	            		  area: ['350px', '195px'], //宽高
	            		  shadeClose: false, //开启遮罩关闭
	            		  content: data
	            		});
				},
				error : function(jqXHR, textStatus, errorThrown) {
					alert("error");
					alert(jqXHR.responseText);
				}
			});
		}
		
		function noSelectUpdate(id) {
		    if(!id) {
		    	alert("ID为"+id);
		    	return;
		    }
			$.ajax({
				type : "POST",
				url : "user/preUpdate",
				data : {"userId":id},
				cache : false,
				timeout : 600000,
				success : function(data) {
					layer.open({
	            		  type: 1,
	            		  title: '修改用户信息页面',
	            		  skin: 'layui-layer-rim', //样式类名
	            		  anim: 2,
	            		  area: ['400px', '445px'], //宽高
	            		  shadeClose: false, //开启遮罩关闭
	            		  content: data,
	            		  btn: '关闭',
	            		  yes: function(){
	            	          layer.closeAll();
	            	        }
	            		});
				},
				error : function(jqXHR, textStatus, errorThrown) {
					alert("error");
					alert(jqXHR.responseText);
				}
			});
		}
		
		function closeWindow() {
			layer.closeAll('page');
		}
	
		$("#cbAll").click(function(){ 
			 if(this.checked){    
		       $("input[name='cbOne']").prop("checked",true);	 
		    }else{    
		       $("input[name='cbOne']").prop("checked",false);
		    }    

		});
	</script>
</body>
</html>
